package br.ufrgs.meetapp.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class PopupMessage {
	
	private AlertDialog alert;

	
	/***
	* Creates a popup with the values passed <br>
	* Your activity should implement DialogInterface.OnClickListener in order to handle the button clicks <br><br>
	*<pre>
	*<b>Example:</b>
	* 
	*public void onClick(DialogInterface dialog, int which)</b> {
	*	Toast toast = Toast.makeText(this, "nenhum dos dois", Toast.LENGTH_LONG);
	*	if(which == dialog.BUTTON1)
	*		toast = Toast.makeText(this, "opcoes", Toast.LENGTH_LONG);
	*	else if(which == dialog.BUTTON2)
	*		toast = Toast.makeText(this, "cancelar", Toast.LENGTH_LONG);
	* toast.show();
	*}
	*</pre>
	* 
	* @param context - activity context
	* @param title - popup title
	* @param message - message that will be displayed
	* @param firstButtonName - lable of the first button
	* @param firstButtonListener - listener that handles clicks on the first button
	*/
	public PopupMessage(Context context, String title, String message, String firstButtonName, DialogInterface.OnClickListener firstButtonListener) {
		this(context, title, message, firstButtonName, null, firstButtonListener, null);
	}

	/***
	* Creates a popup with the values passed <br>
	* Your activity should implement DialogInterface.OnClickListener in order to handle the button clicks <br><br>
	*<pre>
	* <b>Example:</b>
	* 
	*<b>public void onClick(DialogInterface dialog, int which) {
	*	Toast toast = Toast.makeText(this, "nenhum dos dois", Toast.LENGTH_LONG);
	*	if(which == dialog.BUTTON1)
	*		toast = Toast.makeText(this, "opcoes", Toast.LENGTH_LONG);
	*	else if(which == dialog.BUTTON2)
	*		toast = Toast.makeText(this, "cancelar", Toast.LENGTH_LONG);
	* toast.show();
	*}
	*</pre>
	* 
	* @param context - activity context
	* @param title - popup title
	* @param message - message that will be displayed
	* @param firstButtonName - lable of the first button
	* @param secondButtonName - lable of the second button
	* @param firstButtonListener - listener that handles clicks on the first button
	* @param secondButtonListener - listener that handles clicks on the second button
	*/
	public PopupMessage(Context context, String title, String message, String firstButtonName, String secondButtonName, DialogInterface.OnClickListener firstButtonListener, DialogInterface.OnClickListener secondButtonListener) {
				
		try {
			
			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle(title);
			builder.setCancelable(false);
			builder.setMessage(message);
			
			if((firstButtonName != null) && (!firstButtonName.equalsIgnoreCase("")))
				builder.setPositiveButton(firstButtonName, firstButtonListener);
			
			if((secondButtonName != null) && (!secondButtonName.equalsIgnoreCase(""))) {
				builder.setNegativeButton(secondButtonName, secondButtonListener);
			}
			
			alert = builder.create();
			
			
		} catch(Exception exception) {
			System.out.println("Error in PopupMessage.PopupMessage - trying to create new dialog - ");
			if(exception.getMessage() != null) 
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
	}

	/**
	 * Shows the popup in screen
	 * @return true if went ok
	 */
	public boolean display() {
		
		boolean displayed = false;
		//show Dialog
		try {
			alert.show();			
		} catch(Exception exception) {
			System.out.println("Error in PopupMessage.display - trying to display the message - ");
			if(exception.getMessage() != null)
				System.out.println(exception.getMessage());
			exception.printStackTrace();
		}
		
		return displayed;
	}
	
	/**
	 * Hides the popup - i dont think this should be used
	 */
	public void dismiss() {
		if(alert != null)
			alert.dismiss();
	}
}
